package andev.aandroidlib.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 跑马灯TextView
 */
public class MarqueeTextView extends TextView {
    private boolean mKeepMarquee = false;

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Init();
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Init();
    }

    public MarqueeTextView(Context context) {
        super(context);
        Init();
    }

    private void Init() {
        setFocusable(true);
        setFocusableInTouchMode(true);

        setSingleLine();
        setHorizontallyScrolling(true);

        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
    }

    /**
     * @return 重写意图——by default,the 'isFocused' function will only return true if the view is focused.
     * I use mKeepMarquee variable to make sure it will return true if we want to keep
     * marquee even if the view is not focused
     */
    @Override
    public boolean isFocused() {
        return mKeepMarquee ? mKeepMarquee : super.isFocused();
    }

    /**
     * @param keepMarquee true to make the TextView always marquee unless 'stopMarquee' has been called,false to let system to choose whether to marquee or not
     */
    public void startMarquee(boolean keepMarquee) {
        mKeepMarquee = keepMarquee;
        requestFocus();
    }

    public void stopMarquee() {
        mKeepMarquee = false;
    }
}
